package com.herench.bridge;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.springframework.util.Assert;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

public class WsClientFactory {

    private static final WsClientFactory instance = new WsClientFactory();
    private JaxWsDynamicClientFactory factory;

    private static ConcurrentMap<String, Client> clientPool = new ConcurrentHashMap<>();

    private WsClientFactory() {
        this.factory = JaxWsDynamicClientFactory.newInstance();
    }

    public static WsClientFactory getInstance() {
        return instance;
    }

    public synchronized Client getWsClient(String url) {
        Assert.notNull(url, "url is null");
        Client client = clientPool.get(url);
        if (client == null) {
            client=  factory.createClient(url);
            clientPool.put(url,client);
        }
        return client;
    }

    public synchronized void destoryClient(String url){
        clientPool.remove(url);
    }



}